package interfaceTest;

import java.util.Arrays;
import java.util.Comparator;

class Person implements Comparable<Person> {
    public String name;
    public int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Person o) {
        return this.age - o.age;
    }
}

class NameComparator implements Comparator<Person> {

    @Override
    public int compare(Person o1, Person o2) {
        return o1.name.compareTo(o2.name);
    }
}

class AgeComparator implements Comparator<Person> {
    @Override
    public int compare(Person o1, Person o2) {
        return o1.age - o2.age;
    }
}

public class Test3 {
    //接口使用实例
    //类与类之间的比较 接口 Comparable Comparator

    public static void bubbleSort(Person[] persons) {
        if (persons == null) {
            return;
        }
        for (int i = 0; i < persons.length - 1; i++) {
            for (int j = 0; j < persons.length - 1 - i; j++) {
                if (persons[j].compareTo(persons[j + 1]) > 0) {
                    Person tmp = persons[j];
                    persons[j] = persons[j + 1];
                    persons[j + 1] = tmp;
                }
            }
        }

    }

    public static void main(String[] args) {
        //通过compareTo 实现 bubbleSort
        Person person1 = new Person("a", 56);
        Person person2 = new Person("b", 18);
        Person person3 = new Person("c", 28);
        Person[] persons = new Person[]{person1, person2, person3};

        System.out.println("sort before: " + Arrays.toString(persons));
        bubbleSort(persons);
        System.out.println("sort age: " + Arrays.toString(persons));
        //sort before: [{name='a', age=56}, {name='b', age=18}, {name='c', age=28}]
        //sort age: [{name='b', age=18}, {name='c', age=28}, {name='a', age=56}]
    }

    public static void main2(String[] args) {
        //当我们的需求不仅仅是一个按年龄排序时又该咋办呢?
        //比较器 Comparator
        Person person1 = new Person("fbc", 56);
        Person person2 = new Person("bde", 18);
        Person person3 = new Person("apex", 28);
        Person[] persons = new Person[]{person1, person2, person3};
        System.out.println("sort before: " + Arrays.toString(persons));
        Arrays.sort(persons, new NameComparator());
        System.out.println("sort name: " + Arrays.toString(persons));

        Arrays.sort(persons, new AgeComparator());
        System.out.println("sort age: " + Arrays.toString(persons));
        /*
         * sort before: [{name='fbc', age=56}, {name='bde', age=18}, {name='apex', age=28}]
         * sort name: [{name='apex', age=28}, {name='bde', age=18}, {name='fbc', age=56}]
         * sort age: [{name='bde', age=18}, {name='apex', age=28}, {name='fbc', age=56}]
         */

    }

    public static void main1(String[] args) {
        //如何比较自定义的类呢?
        Person person1 = new Person("naowang", 56);
        Person person2 = new Person("xiaomi", 18);
        Person person3 = new Person("zhangnaos", 28);
        Person[] persons = new Person[]{person1, person2, person3};

        System.out.println("sort before: " + Arrays.toString(persons));
        //Arrays.sort(persons);//直接报错 ClassCastException
        //此时是因为我们没有告诉编译器该怎样比较Person这个类
        Arrays.sort(persons);
        //当我们给Person实现 Comparable<Person> 这个接口 并重写compareTo后程序便能正确执行了

        System.out.println("sort after:  " + Arrays.toString(persons));
        //sort before: [{name='naowang', age=56}, {name='xiaomi', age=18}, {name='zhangnaos', age=28}]
        //sort after:  [{name='xiaomi', age=18}, {name='zhangnaos', age=28}, {name='naowang', age=56}]

    }

}
